package worldinruin.gui.imp.gameview;


import java.awt.Dimension;

import javax.swing.Action;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.LayoutStyle;

import worldinruin.GameDataHandler;
import worldinruin.controllers.ControlFactory;
import worldinruin.controllers.ControlFactory.Controllers;
import worldinruin.core.IBuilding;
import worldinruin.core.imp.buildings.BuildingTypes;
import worldinruin.resources.Components;
import worldinruin.resources.IconMap;
import worldinruin.utils.ResizableLabel;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class SelectedBuildingPanel extends javax.swing.JPanel {
	private ResizableLabel iconLabel;
	private JButton trainViewButton;
	private JLabel productionRateLabel;
	private JLabel buildingDescription;
	private JLabel buildingNameLabel;
	private IBuilding building;
	private Action listener;
	
	/**
	 * Creates a selected building panel which display a building on gameboard.
	 * @param building Building to display.
	 */
	public SelectedBuildingPanel(IBuilding building) {
		super();
		initGUI();
		setBuilding(building);
		setupGUI();
		initListener();
	}
	
	/**
	 * Initiates listeners to components.
	 */
	private void initListener() {
		listener = ControlFactory.getControl(Controllers.GAMEBOARD_CONTROLLER);
		trainViewButton.addActionListener(listener);
		listener.putValue(Components.trainSoldiersViewButton, trainViewButton);
		
	}
	
	/**
	 * Sets a building object to the GUI.
	 * @param building Building object. 
	 */
	private void setBuilding(IBuilding building) {
		this.building = building;
	}
	
	/**
	 * Setup the GUI.
	 */
	private void setupGUI(){
		setBuilding(building);
		iconLabel.setImage(IconMap.getInstance().getIcon(building.getBuildingType()).getImage());
		buildingDescription.setText("<html><center>" + building.getBuildingDescription());
		buildingNameLabel.setText("<html><center><h1>" + building.getName());
		setActions();
	}
	
	/**
	 * Sets components according to the building object.
	 */
	public void setActions() {
		if(building.getBuildingType() == BuildingTypes.CITY ||
				building.getBuildingType() == BuildingTypes.BARRACK) {
			//trainViewButton.setVisible(GameDataHandler.getInstance().getGame().getActivePlayer().getBuildings().contains(building));
			trainViewButton.setVisible(true);
			productionRateLabel.setVisible(false);
		}
		else {
			trainViewButton.setVisible(false);
			productionRateLabel.setVisible(true);
			productionRateLabel.setText("Produces " + building.getName());
		}
	}
	
	/**
	 * Initializes components in GUI.
	 */
	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)this);
			this.setLayout(thisLayout);
			setPreferredSize(new Dimension(400, 300));
			{
				iconLabel = new ResizableLabel();
			}
			{
				buildingDescription = new JLabel();
			}

			{
				productionRateLabel = new JLabel();
				productionRateLabel.setText("Production rate: ");
			}
			{
				trainViewButton = new JButton();
				trainViewButton.setText("Train units");
			}
			{
				buildingNameLabel = new JLabel();
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addGap(7)
				.addComponent(buildingNameLabel, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(buildingDescription, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE)
				        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				        .addGroup(thisLayout.createParallelGroup()
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(productionRateLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				                .addGap(21))
				            .addGroup(thisLayout.createSequentialGroup()
				                .addComponent(trainViewButton, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
				                .addGap(0, 0, Short.MAX_VALUE))))
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addGap(14)
				        .addComponent(iconLabel, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE)
				        .addGap(6)))
				.addContainerGap(137, 137));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addComponent(iconLabel, GroupLayout.PREFERRED_SIZE, 114, GroupLayout.PREFERRED_SIZE)
				.addGap(18)
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(productionRateLabel, 0, 122, Short.MAX_VALUE)
				        .addGap(7))
				    .addGroup(thisLayout.createSequentialGroup()
				        .addComponent(trainViewButton, GroupLayout.PREFERRED_SIZE, 128, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 0, Short.MAX_VALUE))
				    .addGroup(thisLayout.createSequentialGroup()
				        .addComponent(buildingDescription, GroupLayout.PREFERRED_SIZE, 129, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 0, Short.MAX_VALUE))
				    .addGroup(thisLayout.createSequentialGroup()
				        .addComponent(buildingNameLabel, GroupLayout.PREFERRED_SIZE, 128, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 0, Short.MAX_VALUE)))
				.addContainerGap(127, 127));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
